
import { createRouter, createWebHashHistory } from 'vue-router'

import Login from '../login'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            component: Login,
        },
        {
            path: '/error',
            component: () => import('../error'),
        },
        {
            path: '/home',
            component: () => import('../home'),
        },
    ]
})

router.beforeEach((to, from) => {
    let Info = JSON.parse(sessionStorage.getItem('info'))
    console.log(Info);
    console.log('to=>', to);
    console.log('from=>', from);
    if (to.path == '/home') {
        if (Info) {
            if (Info.acc == 'admin' && Info.pwd == 'admin') {
                return true
            } else {
                return {path:'error'}
            }
        } else return {path:'/'}
    } else if (to.path == '/error') {
        if (Info) {
            if (Info.acc == 'admin' && Info.pwd == 'admin') {
                return { path: '/home' }
            } else {
                return true
            }
        } else return {path:'/'}
    } else if (to.path == '/') {
        if (Info) {
            if (Info.acc == 'admin' && Info.pwd == 'admin') {
                return { path: '/home' }
            } else {
                return true
            }
        } else return true
    }else return true
})


export default router